package main

import (
	"log"
	"strconv"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

func main() {
	dingo.InitViper("./configs/adm.yaml")
	dbs.NewMysqlComponent().Step()

	// 获取南宁分公司下的所有门店
	userId := 221
	userIds := GetUserIds(userId)
	placeIds := GetPlaceListByUserIdsToChangePrice(userIds)

	var ids string
	for _,id := range placeIds {
		ids = ids + "," + strconv.Itoa(id)
	}
	log.Println(ids)
	//log.Println(placeIds)
	// 去除套餐门店






	//lists := GetPlaceListByFeeMode(1)
	////lists2 := GetPlaceListByFeeMode(2)
	//var data,data8,data10,data12 []int
	//for _,lst := range lists {
	//	priceInfo := GetPriceInfoByPlaceId(lst.PlaceId)
	//	if priceInfo != nil {
	//		if priceInfo.Deposit < 1000 { // 小于10押金
	//			if priceInfo.Deposit == 800 {
	//				priceInfo.Id = 0
	//				priceInfo.Deposit = 1500
	//				priceInfo.MaxPayPrice = 1500
	//				priceInfo.CreateAt = dingo.Time(time.Now().Unix())
	//				priceInfo.Name = "2022年春节"
	//				priceInfo.ProductId = utils.Str2Md5(helper.GenerateUUID())
	//				if b := InsertIntoPlacePrice(priceInfo); b {
	//					data8 = append(data8, priceInfo.PlaceId)
	//				}
	//			} else {
	//				data = append(data, priceInfo.PlaceId)
	//			}
	//		} else {
	//			if priceInfo.Deposit == 1000 {
	//				priceInfo.Id = 0
	//				priceInfo.Deposit = 1500
	//				priceInfo.MaxPayPrice = 1500
	//				priceInfo.CreateAt = dingo.Time(time.Now().Unix())
	//				priceInfo.Name = "2022年春节"
	//				priceInfo.ProductId = utils.Str2Md5(helper.GenerateUUID())
	//				if b := InsertIntoPlacePrice(priceInfo); b {
	//					data10 = append(data10, priceInfo.PlaceId)
	//				}
	//			}
	//			if priceInfo.Deposit == 1200 {
	//				priceInfo.Id = 0
	//				priceInfo.Deposit = 1500
	//				priceInfo.MaxPayPrice = 1500
	//				priceInfo.CreateAt = dingo.Time(time.Now().Unix())
	//				priceInfo.Name = "2022年春节"
	//				priceInfo.ProductId = utils.Str2Md5(helper.GenerateUUID())
	//				if b := InsertIntoPlacePrice(priceInfo); b {
	//					data12 = append(data12, priceInfo.PlaceId)
	//				}
	//			}
	//		}
	//	}
	//}
	//log.Println("data", data)
	//log.Println("data8-押金为8元的门店", data8)
	//log.Println("data10-押金为10元的门店", data10)
	//log.Println("data12-押金为12元的门店", data12)
}

// 获取下级所有用户
func GetUserIds(id int) []int {
	isLoop := true
	var userIds, ids []int
	ids = append(ids, id)
	for isLoop {
		tempUserIds := GetUserIdsByParentId(ids)
		if len(tempUserIds) == 0 {
			isLoop = false
		} else {
			userIds = append(userIds, tempUserIds...)
			ids = tempUserIds
		}
	}
	return userIds
}

// 直属下级数
func GetUserIdsByParentId(parentIds []int) []int {
	var userIds []int
	global.Orm.Table("users").
		Select("id").
		Where("parent_id in (?)", parentIds).Find(&userIds)
	return userIds
}

// 获取门店ID
func GetPlaceListByUserIdsToChangePrice(ids []int) []int {
	var total []int
	global.Orm.Table("places").
		Select("id").
		Where("user_id in (?)", ids).
		Find(&total)
	return total
}
